// ----------------------------------
// RSDK Project: Sonic 2
// Script Description: Spiny Wall Object
// Script Author: Christian Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

// ========================
// Aliases
// ========================

private alias object.value0 : object.startPos.x
private alias object.value1 : object.startPos.y
private alias object.value2 : object.dirChangeTimer
private alias object.value3 : object.shootTimer
private alias object.value4 : object.shotVelocity
private alias object.value5 : object.targetDistance

private alias 0 : SPINYWALL_MOVING
private alias 1 : SPINYWALL_IDLE

// Player Aliases
private alias object.xpos : player.xpos
private alias object.value40 : player.hitboxLeft
private alias object.value38 : player.hitboxTop
private alias object.value41 : player.hitboxRight
private alias object.value39 : player.hitboxBottom


// ========================
// Function Declarations
// ========================

reserve function SpinyWall_Setup
reserve function SpinyWall_DebugDraw
reserve function SpinyWall_DebugSpawn



// ========================
// Function Definitions
// ========================

private function SpinyWall_Setup
	object[arrayPos0].xpos = object[arrayPos0].startPos.x
	object[arrayPos0].ypos = object[arrayPos0].startPos.y
	object[arrayPos0].dirChangeTimer = 256
	object[arrayPos0].shootTimer = 128
	object[arrayPos0].frame = 0
	object[arrayPos0].animationTimer = 0
	object[arrayPos0].state = 0
	object[arrayPos0].direction = object[arrayPos0].propertyValue
	object[arrayPos0].yvel = -0x4000
	object[arrayPos0].priority = PRIORITY_BOUNDS
end function


private function SpinyWall_DebugDraw
	DrawSpriteFX(0, FX_FLIP, object.xpos, object.ypos)
end function


private function SpinyWall_DebugSpawn
	CreateTempObject(TypeName[Spiny Wall], temp0, object.xpos, object.ypos)
	object[tempObjectPos].startPos.x = object.xpos
	object[tempObjectPos].startPos.y = object.ypos
	arrayPos0 = object[tempObjectPos].entityPos
	object[tempObjectPos].propertyValue = object.direction
	CallFunction(SpinyWall_Setup)
end function


// ========================
// Events
// ========================

event ObjectUpdate
	object.priority = PRIORITY_ACTIVE
	switch object.state
	case SPINYWALL_MOVING
		object.dirChangeTimer--
		if object.dirChangeTimer <= 0
			object.dirChangeTimer = 256
			object.shootTimer = 128
			FlipSign(object.yvel)
			object.direction ^= FLIP_Y
		end if

		object.ypos += object.yvel

		object.animationTimer++
		if object.animationTimer == 10
			object.animationTimer = 0
			object.frame ^= 1
		end if

		if object.shootTimer == 0
			object.targetDistance = 0x7FFFFFFF
			foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
				BoxCollisionTest(C_TOUCH, object.entityPos, -96, -256, 96, 256, currentPlayer, 1, 1, 1, 1)
				if checkResult == true
					object.shootTimer = 40
					object.frame = 2
					object.state = 1
					temp0 = object.xpos
					temp0 -= player[currentPlayer].xpos
					Abs(temp0)
					if temp0 < object.targetDistance
						object.targetDistance = temp0
						if player[currentPlayer].xpos > object.xpos
							object.shotVelocity = 0x30000
						else
							object.shotVelocity = -0x30000
						end if
					end if
				end if
			next
		else
			object.shootTimer--
		end if
		break

	case SPINYWALL_IDLE
		object.shootTimer--
		if object.shootTimer == 20
		end if

		if object.shootTimer < 0
			object.shootTimer = 64
			object.frame = 0
			object.state = 0
		end if
		break
		


	end switch

	if object.outOfBounds == true
		temp0 = object.xpos
		temp1 = object.ypos
		object.xpos = object.startPos.x
		object.ypos = object.startPos.y
		if object.outOfBounds == true
			arrayPos0 = object.entityPos
			CallFunction(SpinyWall_Setup)
		else
			object.xpos = temp0
			object.ypos = temp1
		end if
	end if

	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		BoxCollisionTest(C_TOUCH, object.entityPos, -8, -8, 8, 8, currentPlayer, player[currentPlayer].hitboxLeft, player[currentPlayer].hitboxTop, player[currentPlayer].hitboxRight, player[currentPlayer].hitboxBottom)
		if checkResult == true
			CallFunction(Player_BadnikBreak)
		end if
	next
end event


event ObjectDraw
	DrawSpriteFX(object.frame, FX_FLIP, object.xpos, object.ypos)
end event


event ObjectStartup
	LoadSpriteSheet("EHZ/Objects2.gif") // this was probably meant to be removed when MBZ became a thing, makes no real difference whether its here or not though

	CheckCurrentStageFolder("Zone01")
	if checkResult == true
		LoadSpriteSheet("EHZ/Objects2.gif")
		SpriteFrame(-12, -24, 23, 48, 1, 25)
		SpriteFrame(-12, -22, 23, 44, 25, 25)
                SpriteFrame(-12, -24, 23, 48, 1, 25)
	else
		LoadSpriteSheet("MBZ/Objects.gif")
		SpriteFrame(-12, -24, 23, 48, 99, 501)
		SpriteFrame(-12, -22, 23, 44, 99, 550)
		SpriteFrame(-12, -24, 23, 48, 99, 595)
	end if

	foreach (TypeName[Spiny Wall], arrayPos0, ALL_ENTITIES)
		object[arrayPos0].startPos.x = object[arrayPos0].xpos
		object[arrayPos0].startPos.y = object[arrayPos0].ypos
		CallFunction(SpinyWall_Setup)
	next

	SetTableValue(TypeName[Spiny Wall], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(SpinyWall_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(SpinyWall_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event


// ========================
// Editor Events
// ========================

event RSDKEdit
	if editor.returnVariable == true
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			checkResult = object.propertyValue
			break
			
		case 0 // direction
			GetBit(checkResult, object.propertyValue, 1)
			break
			
		end switch
	else
		switch editor.variableID
		case EDIT_VAR_PROPVAL // property value
			object.propertyValue = editor.variableValue
			break
			
		case 0 // direction
			CheckNotEqual(editor.variableValue, 0)
			SetBit(object.propertyValue, 1, checkResult)
			break
			
		end switch
	end if
end event


event RSDKDraw
	object.direction = object.propertyValue
	DrawSpriteFX(0, FX_FLIP, object.xpos, object.ypos)
end event


event RSDKLoad
	CheckCurrentStageFolder("Zone01")
	if checkResult == true
		LoadSpriteSheet("EHZ/Objects2.gif")
		SpriteFrame(-12, -24, 23, 48, 1, 25)
	else
		LoadSpriteSheet("EHZ/Objects2.gif")
		SpriteFrame(-12, -24, 23, 48, 99, 501)
	end if

	AddEditorVariable("direction")
	SetActiveVariable("direction")
	AddEnumVariable("Left", 0)
	AddEnumVariable("Right", 1)
end event
